<?php
namespace app\index\controller;
use think\Controller;
use app\common\model\Article;
use app\common\model\Site;
use app\common\model\SiteCategory;

class Sitemap extends Controller
{
    public function index() {
        $site_url = httpProtocolHeader().$_SERVER['HTTP_HOST'];
        $str = '<?xml version="1.0" encoding="utf-8"?>'.PHP_EOL;
        $str .= '<!--  本XML文件由系统自动生成 武汉类森科技有限公司 https://www.laysns.com  -->'.PHP_EOL;
        $str .= '<urlset>'.PHP_EOL;

        $articles = (New Article())->where('status',1)->order('total_view','desc')->column('id,created_at');
        $i=0;
        foreach ($articles as $k => $v) {
            $priority=($i<=10?'1.0':($i<=50?'0.8':'0.7'));
            $str .= '<url>';
            $str .= '<loc>'.$site_url.'/html/'.$k.'.html</loc>'.PHP_EOL;
            $str .= '<lastmod>'.$v.'</lastmod>'.PHP_EOL;
            $str .= '<priority>'.$priority.'</priority >'.PHP_EOL;
            $str .= '<changefreq>daily</changefreq>'.PHP_EOL.PHP_EOL.PHP_EOL;
            $str .= '</url>';
            $i++;
        }

        $articles = (New Site())->where('status',1)->order('total_view','desc')->column('id,joined_at');
        $i=0;
        foreach ($articles as $k => $v) {
            $priority=($i<=10?'1.0':($i<=50?'0.8':'0.7'));
            $str .= '<url>';
            $str .= '<loc>'.$site_url.'/site/'.$k.'.html</loc>'.PHP_EOL;
            $str .= '<lastmod>'.$v.'</lastmod>'.PHP_EOL;
            $str .= '<priority>'.$priority.'</priority >'.PHP_EOL;
            $str .= '<changefreq>daily</changefreq>'.PHP_EOL.PHP_EOL.PHP_EOL;
            $str .= '</url>';
            $i++;
        }
        $str .='</urlset>'.PHP_EOL;
        file_put_contents(APP_PATH."/../sitemap.xml", $str);
        echo '网站地图更新成功！可以到网站根目录下的<a target="_blank" href="'.$site_url.'/sitemap.xml">sitemap.xml</a>查看！';
    }
}
